package com.steven.sync.mianshi;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 一个线程打印 A-Z 一个线程打印1-26 要交叉打印A1B2C3....Z26
 */
public class Test3_notify_A1B2C3 {


    public static void main(String[] args) {

        //门闩控制哪个数字先打印
        CountDownLatch countDownLatch = new CountDownLatch(1);

        String array[] = {"A", "B", "C", "D", "E", "F"};
        String array2[] = {"1", "2", "3", "4", "5", "6"};
        final Object o = new Object();
        new Thread(() -> {
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o) {
                for (String item : array) {
                    try {
                        System.out.println(item);
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify(); //不加这个会使线程不会结束
            }
        }).start();

        new Thread(() -> {
            synchronized (o) {
                for (String item : array2) {
                    try {
                        System.out.println(item);
                        countDownLatch.countDown();
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify(); //不加这个会使线程不会结束
            }
        }).start();

    }
}
